Mongodb to SQL Migration 플랜

기존의 고객 데이터베이스들을 SQL 기반 DB로 옮기는 것 까지는 기정사실이긴 한데, 구체적으로 어느 SQL로 가야 하는가? Postgres, MySQL, MariaDB, Oracle, Microsoft, SQLite 이렇게 다양한데 판단의 기준을 세워야 할 것 같다.

판단의 기준

1. 가격

엔터프라이즈 DB는 정말 더럽게 비싸다. 목표는 MongoDB보다 저렴하고 AWS 비용까지 고려했을 때 합리적인 가격이어야 한다.

https://aws.amazon.com/rds/pricing/

DB Type Instances Price Per Hour
RDS for Oracle License Included (LI) Single-AZ db.t3.small $0.088
RDS for Oracle License Included (LI) Multi-AZ db.t3.small $0.176
RDS for Oracle BYOL [1] Single-AZ Deployment db.t3.small $0.052
RDS for Oracle BYOL [1:1] Multi-AZ Deployment db.t3.small $0.104
RDS for MySQL Single-AZ Deployment db.t4g.micro $0.025
RDS for MySQL Single-AZ Deployment db.t4g.small $0.051
RDS for MySQL Multi-AZ Deployment db.t4g.micro $0.051
RDS for MySQL Multi-AZ Deployment db.t4g.small $0.102
Amazon RDS for PostgreSQL Single-AZ Deployment db.t4g.micro $0.025
Amazon RDS for PostgreSQL Single-AZ Deployment db.t4g.small $0.051
Amazon RDS for PostgreSQL Multi-AZ Deployment db.t4g.micro $0.051
Amazon RDS for PostgreSQL Multi-AZ Deployment db.t4g.small $0.102
RDS for MariaDB Single-AZ Deployment db.t4g.micro $0.025
RDS for MariaDB Single-AZ Deployment db.t4g.small $0.051
RDS for MariaDB Multi-AZ Deployment db.t4g.micro $0.051
RDS for MariaDB Multi-AZ Deployment db.t4g.small $0.102
RDS for SQL Server On-Demand Instances db.t3.micro $0.031
RDS for SQL Server On-Demand Instances db.t3.small $0.062

AWS Aurora는 MySQL, PostgreSQL 호환이 되는 고성능 데이터베이스 서비스이다. 두가지 버전이 있는데, IO에 요금을 부과하는 Aurora Standard와 스토리지 저장용량에 요금을 부과하는 Aurora I/O-Optimized 서비스가 존재한다.

AWS Aurora는 다양한 Capacity 옵션이 존재한다. Serverless, Provisioned On-Demand Instance, Provisioned Reserved Instance, Limitless.

AWS Aurora Serverless 옵션은 서버리스 특성상 자동으로 스케일링을 조절한다. 요청이 없으면 스스로 꺼지고 갑자기 많은 양의 트랜잭션이 요청되면 자원을 수평적으로 할당한다. Provisioned 옵션과 달리 항시 켜져있지 않기 때문에 사용시간을 특별한 ACU (Aurora Capacity Units) 단위를 가지고 요금을 책정하는데, 1초 단위로 아주 정밀하게 요금을 계산한다.

Measure Aurora Standard (per ACU hour) Aurora I/O-Optimized (per ACU hour)
Aurora Capacity Unit $0.20 $0.26

서버리스가 싫으면 Provisioned On-Demand Instance 혹은 Provisioned Reserved Instance를 사용하면 된다. 전자는 일반적인 RDS 인스턴스를 월 단위로 지불하는 방식이고, Reserved Instance는 한 번에 몇개월/몇년치 금액을 지불하여 쓰는 방식이다.

Standard Instances - Current Generation Aurora Standard (Price Per Hour) Aurora I/O-Optimized (Price Per Hour)
db.t4g.medium $0.113 $0.147
db.t4g.large $0.227 $0.295
db.t3.medium $0.125 $0.163
db.t3.large $0.25 $0.325

2. 기술지원

돈을 많이 내면 (엔터프라이즈 요금제를 사용하면) 각종 기술지원 및 사후지원이 제공된다. PostgreSQL도 엔터프라이즈 고객에 대한 기술지원이 제공된다고 하는데, 확실히 날 것 그대로의 오픈소스 보다는 기술보증이 탄탄하니까 돈이 오가는 크리티컬한 업계라면 고려해볼만하다.

3. 성능

어차피 NoSQL에서 넘어오는데... 아무 SQL DB를 사용해도 성능향상은 보장된다.

4. ORDB

PostgreSQL 때문에 고민이다. 얘는 그냥 RDBMS가 아니라 ORDBMS라서 "객체" 개념이 들어간다. Schema에 Function이 달릴 수 있는 영락없는 객체라고. 이걸로 어떤 차별성을 가질 수 있는지도 잘 모르겠고, 우리 도메인에 ORDB가 필요한지도 알아내야 한다.

5. Type

Enum, Uuid, Decimal, Json, Array같은 타입을 우리 서비스에서 사용하는가? 그러면 가능한 최대한 많은 타입을 지원하는 DB를 써야 유리할 것이다. 안 그러면 마이그레이션할 때 변환 로직을 추가해야 할 것이다.

AI Response :: deepseek-r1:14b

Thought for 140 seconds > Okay, so I'm trying to figure out how to help someone migrate their data from MongoDB to a SQL-based database. I don't have a lot of experience with migrations, especially between such different systems as NoSQL and relational databases. But I'll try to break it down step by step. > > First, I know that MongoDB is schema-less, while SQL databases are structured with tables and columns. So the first challenge is understanding how to map MongoDB's documents into SQL tables. Each document in MongoDB could potentially become a row in an SQL table, but since MongoDB allows for nested documents, I might need to normalize those into separate tables or denormalize them into a single table if that makes sense. > > I should start by assessing the current MongoDB schema and data. What collections do they have? How are the documents structured? Are there any relationships between the documents? For example, if there's a field in one document that refers to another document, I might need to create foreign keys in SQL tables for those relationships. > > Next, choosing the right SQL database is important. The options include MySQL, PostgreSQL, SQL Server, etc., each with their own strengths and weaknesses. Maybe factors like performance, scalability, cost, and community support will influence this decision. If the application currently runs on a specific ecosystem, that might be a good reason to choose a corresponding SQL database. > > Designing the target SQL schema is crucial. I need to map MongoDB collections to SQL tables. For example, each collection could become a table with an auto-incremented primary key. But what about embedded documents? Should they be flattened into the same table or split into separate tables? Probably splitting them makes more sense for relational databases, but that might complicate queries. > > I also need to handle data types. MongoDB has specific data types like ObjectId, Date, etc., which I'll have to map to their SQL equivalents. For instance, ObjectId could be stored as a string or perhaps converted into another type if necessary. > > Now, about the migration process itself. The user should ensure they back up both MongoDB and SQL databases before doing anything risky. Maybe set up a staging environment where they can test the migration without affecting production data. > > Extracting data from MongoDB might involve writing some scripts using tools like mongodump or maybe using an ORM if it's part of an application. Then, transforming that data into a format suitable for SQL. This could be CSV files or directly inserting into the SQL database. But wait, dealing with complex nested documents might require more sophisticated processing. > > Loading the transformed data into SQL is next. They can use tools like mongodump to export data and then import it using SQL utilities or write custom scripts. Maybe they'll need ETL (Extract, Transform, Load) processes for this step, especially if the data needs significant restructuring. > > Validation is important too. After migration, they should check that all data has been transferred correctly and that there are no discrepancies in the schema. Running some queries to compare results between MongoDB and SQL would be a good idea. > > Testing the application after migration is essential. They'll need to ensure that all functionality works as expected, especially any parts of the application that interact with the database. Stress testing might also help identify performance issues or bottlenecks introduced during migration. > > Optimization will likely come next. This could involve creating indexes on SQL tables if certain queries are slow, adjusting query patterns to fit SQL's strengths, and possibly normalizing or denormalizing data for better performance. > > Documentation is something they shouldn't overlook. Keeping track of the migration process, any issues faced, and how they were resolved can be helpful for future reference or if there's a need to troubleshoot later on. > > Finally, post-migration support is necessary in case anything goes wrong after deployment. They should monitor the SQL database closely for performance metrics and ensure that all data integrity constraints are met. > > I think I might have missed some steps or perhaps overcomplicated things. Maybe they could also look into using migration tools like mongomigrate or other ETL tools that support moving data from MongoDB to SQL databases directly. Also, considering any potential issues with data size—MongoDB can handle large datasets, but moving them efficiently to SQL might require careful planning. > > Another thought: if the application is using an ORM, they'll need to adjust the models and queries to fit the new SQL schema. This could involve a lot of code changes, so it's something to plan for. > > I also wonder about any specific features in MongoDB that don't have direct equivalents in SQL, like aggregation pipelines or document updates with atomic operations. How would these translate into SQL? They might need to be handled differently, possibly through stored procedures or more complex queries. > > In summary, the migration process seems pretty involved but manageable if approached step by step. Starting with assessment and planning, then moving on to schema design, data extraction, transformation, loading, validation, testing, optimization, and documentation. Each phase has its own challenges, especially around handling different data structures and ensuring data integrity.
Migrating from MongoDB to a SQL-based database system is a comprehensive process that requires careful planning and execution. Below is a structured approach to facilitate this migration:

1. Assessment and Planning

2. Design Target SQL Schema

3. Extract Data from MongoDB

4. Transform and Load Data

5. Validation

6. Application Testing

7. Optimization

8. Documentation and Support

9. Consider Tools and Features

By following these steps, the migration can be executed methodically, ensuring minimal disruption and successful transition to a SQL-based system.

AI Response :: llama3.1:8b

Migrating from MongoDB to a SQL-based database system involves several steps, including understanding the schema and data structure of your current MongoDB setup, choosing a target SQL-based database (e.g., MySQL, PostgreSQL), designing and implementing the conversion process, testing the new database, and ensuring data integrity. Here's a comprehensive plan to guide you through this transition:

1. Understand Your Current MongoDB Schema

Before migrating to a SQL-based system, it's crucial to understand how your data is structured in MongoDB.

2. Choose Your Target SQL-Based Database

Select the SQL-based database that best fits your needs, considering factors such as:

Popular choices include MySQL, PostgreSQL, Microsoft SQL Server (if on Windows), Oracle Database (for more robust enterprise scenarios), and SQLite for local/small-scale applications.

3. Design Your New SQL Schema

Based on your understanding of MongoDB schema, design a new database schema in your chosen SQL system. Consider:

4. Convert Your MongoDB Data

Implement the actual migration process:

  1. Export Existing Data from MongoDB:

    • Use mongoexport command-line tool or a library like Mongoose in Node.js to export your data into CSV, JSON, or another format suitable for importing into your SQL database.
  2. Prepare SQL Database and Schema:

    • Create the tables with appropriate fields and indexes.
    • Import the exported data into these tables using tools provided by your chosen SQL system (e.g., LOAD DATA INFILE in MySQL).
  3. Transform Complex Data Structures (if necessary):

    • If MongoDB data contains complex structures like embedded documents, arrays, or nested collections, you might need custom scripts to flatten them for easier import into the relational database.

5. Test the New Database

6. Ensure Compatibility and Minimize Downtime (if applicable)

If you're migrating a live application:

Migrating from MongoDB to a SQL-based system requires careful planning and execution. The key is understanding your data model in both systems, choosing an appropriate SQL system, and ensuring that the conversion process maintains integrity and performance.

AI Response :: o3-mini-high

Below is a comprehensive plan for migrating from MongoDB to a SQL-based system using Prisma ORM. This plan assumes you already have a well-structured Prisma schema for MongoDB and outlines the steps from schema conversion to data migration and verification.


1. Assess and Redesign the Data Model


2. Update the Prisma Schema


3. Set Up the SQL Database Environment


4. Plan and Execute Data Migration


5. Update Application Code and Deployment


6. Monitor and Optimize


Summary

This migration plan involves careful redesign of your data model, updating the Prisma schema, setting up a new SQL environment, and writing an ETL process for data migration. Testing at every stage is crucial to ensure data integrity and application stability. Once the migration is complete, updating your application configuration and monitoring performance will ensure a smooth transition from MongoDB to your new SQL-based system.

Feel free to reach out with any follow-up questions or for further details on specific parts of the migration process.



  1. BYOL - Bring Your Own Licence, 이미 해당 라이선스를 소유하고 있을 경우 ↩︎ ↩︎